package com.atguigu.gmall.canal.config;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.atguigu.gmall.canal.model.BaseTrademark;
import com.xpand.starter.canal.annotation.*;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@CanalEventListener
public class MyEventListener {

/*
    @InsertListenPoint
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @UpdateListenPoint
    public void onEvent1(CanalEntry.RowData rowData) {
        System.err.println("UpdateListenPoint");
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @DeleteListenPoint
    public void onEvent3(CanalEntry.EventType eventType) {
        System.err.println("DeleteListenPoint");
    }
*/

    @ListenPoint(destination = "example",
            schema = {"gmall_product"},
            table = {"base_trademark"},
            eventType = CanalEntry.EventType.UPDATE)
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) ->
                System.err.println("Key: " + c.getName() + " ::  Value:: " + c.getValue()));

        switch (eventType){
            case INSERT:
                BaseTrademark insertBaseTrademark = CanalDataHadler.convertToBean(rowData.getAfterColumnsList(), BaseTrademark.class);
                System.out.println("执行了一条插入操作，数据为：" + insertBaseTrademark);
                break;
            case DELETE:
                BaseTrademark deleteBaseTrademark = CanalDataHadler.convertToBean(rowData.getAfterColumnsList(), BaseTrademark.class);
                System.out.println("执行了一条删除操作，数据为：" + deleteBaseTrademark);
                break;
            case UPDATE:
                BaseTrademark updateBaseTrademark = CanalDataHadler.convertToBean(rowData.getAfterColumnsList(), BaseTrademark.class);
                System.out.println("执行了一条更新操作，数据为：" + updateBaseTrademark);
                break;
            default:
                break;
        }
    }
}
